package com.yanqu.road.task;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.server.command.CommandControl;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.queue.ICommandQueue;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.protocol.CrossProtocol;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class AgentCommandTask implements Runnable {
    private static Logger logger = LogManager.getLogger(AgentCommandTask.class.getName());
    private ICommandQueue player;
    private YanQuMessage yanQuMessage;
    private ChannelHandlerContext ctx;

    public AgentCommandTask(ChannelHandlerContext ctx, YanQuMessage yanQuMessage, ICommandQueue player) {
        this.ctx = ctx;
        this.yanQuMessage = yanQuMessage;
        this.player = player;
    }

    @Override
    public final void run() {
        try {
            int code = yanQuMessage.getCode();
            if (code == CrossProtocol.C_CROSS_SERVICE_HANDLER) {
                SyncMessage.Request request = SyncMessage.Request.parseFrom(yanQuMessage.getBytes());
                code = request.getCmdCode();
            }
            RouteRule.route(ctx, yanQuMessage);
        } catch (Exception e) {
            logger.error("执行command失败, message string: {}" , yanQuMessage, e);
        } finally {
            if (null != player) {
                player.getCommandQueue().remove();
            }
        }
    }


}
